﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DBProvider.Providers
{
	public class DrugStoreProvider : IDrugStoreProvider
	{
		#region IDrugStoreProvider Members

		public DrugStore CreateDrugStore(DrugStore theNewDrugStore, out string msg)
		{
			if(theNewDrugStore == null)
			{
				throw new ArgumentOutOfRangeException();
			}

			msg = string.Empty;
			DrugStore result = null;

			try
			{
				using (DBHealthCommunityDataContext db = new DBHealthCommunityDataContext())
				{
					var isExist = db.DrugStores.Count(ds => ds.DrugStoreName == theNewDrugStore.DrugStoreName) > 0;

					if(isExist)
					{
						msg = "同名的药店已存在";
					}
					else
					{
						db.DrugStores.InsertOnSubmit(theNewDrugStore);
						db.SubmitChanges();

						result = db.DrugStores.SingleOrDefault(ds => ds.DrugStoreName == theNewDrugStore.DrugStoreName);
					}
				}
			}
			catch(Exception exc)
			{
				throw exc;
			}

			return result;
		}

		public bool UpdateDrugStore(DrugStore theDrugStore)
		{
			if(theDrugStore == null || theDrugStore.DrugStoreID <= 0)
			{
				throw new ArgumentException();
			}

			var result = false;

			try
			{
				using(DBHealthCommunityDataContext db = new DBHealthCommunityDataContext())
				{
					var dbDrugStore = db.DrugStores.SingleOrDefault(ds => ds.DrugStoreID == theDrugStore.DrugStoreID);

					if(dbDrugStore != null)
					{
						var isExist = db.DrugStores.Count(ds => ds.DrugStoreName == theDrugStore.DrugStoreName && ds.DrugStoreID != theDrugStore.DrugStoreID) > 0;

						if(!isExist)
						{
							dbDrugStore.DrugStoreDescription = theDrugStore.DrugStoreDescription;
							dbDrugStore.DrugStoreName = theDrugStore.DrugStoreName;
							dbDrugStore.LastUpdateTime = theDrugStore.LastUpdateTime;
							dbDrugStore.DrugStorePic = theDrugStore.DrugStorePic;

							db.SubmitChanges();

							result = true;
						}
					}
				}
			}
			catch(Exception exc)
			{
				throw exc;
			}

			return result;
		}

		public bool DeleteDrugStore(int drugStoreID)
		{
			if(drugStoreID <= 0)
			{
				throw new ArgumentOutOfRangeException();
			}

			var result = false;

			try
			{
				using(DBHealthCommunityDataContext db = new DBHealthCommunityDataContext())
				{
					var dbDrugStore = db.DrugStores.SingleOrDefault(ds => ds.DrugStoreID == drugStoreID);

					if(dbDrugStore != null)
					{
						db.DrugStores.DeleteOnSubmit(dbDrugStore);
						db.SubmitChanges();

						result = true;
					}
				}
			}
			catch(Exception exc)
			{
				throw exc;
			}

			return result;
		}

		public DrugStore GetDrugStore(int drugStoreID)
		{
			if(drugStoreID <= 0)
			{
				throw new ArgumentOutOfRangeException();
			}

			DrugStore result = null;

			try
			{
				using(DBHealthCommunityDataContext db = new DBHealthCommunityDataContext())
				{
					result = db.DrugStores.SingleOrDefault(ds => ds.DrugStoreID == drugStoreID);
				}
			}
			catch(Exception exc)
			{
				throw exc;
			}

			return result;
		}

		public DrugStore GetDrugStore(string drugStoreName)
		{
			if(string.IsNullOrEmpty(drugStoreName))
			{
				throw new ArgumentException();
			}

			DrugStore result = null;

			try
			{
				using(DBHealthCommunityDataContext db = new DBHealthCommunityDataContext())
				{
					result = db.DrugStores.SingleOrDefault(ds => ds.DrugStoreName == drugStoreName);
				}
			}
			catch(Exception exc)
			{
				throw exc;
			}

			return result;
		}

		public List<DrugStore> GetAllDrugStore()
		{
			List<DrugStore> result = null;

			try
			{
				using(DBHealthCommunityDataContext db = new DBHealthCommunityDataContext())
				{
					var data = db.DrugStores;

					if(data != null)
					{
						result = data.ToList();
					}
				}
			}
			catch(Exception exc)
			{
				throw exc;
			}

			return result;
		}

		public DrugStoreInfo GetDrugStoreInfo(int drugstoreID)
		{
			if (drugstoreID <= 0)
			{
				throw new ArgumentOutOfRangeException();
			}

			DrugStoreInfo result = null;

			try
			{
				using (DBHealthCommunityDataContext db = new DBHealthCommunityDataContext())
				{
					result = db.DrugStoreInfos.SingleOrDefault(dsi => dsi.DrugStoreID == drugstoreID);
				}
			}
			catch (Exception exc)
			{
				throw exc;
			}

			return result;
		}

		public void SaveDrugStoreInfo(DrugStoreInfo theDrugStoreInfo)
		{
			if (theDrugStoreInfo == null)
			{
				throw new ArgumentNullException();
			}

			try
			{
				using (DBHealthCommunityDataContext db = new DBHealthCommunityDataContext())
				{
					var dbDrugStoreInfo = db.DrugStoreInfos.SingleOrDefault(dsi => dsi.DrugStoreID == theDrugStoreInfo.DrugStoreID);

					if (dbDrugStoreInfo == null)
					{
						if(theDrugStoreInfo.DrugStoreID > 0)
						{
							theDrugStoreInfo.CreateTime = DateTime.Now;
							theDrugStoreInfo.LastUpdate = DateTime.Now;
							db.DrugStoreInfos.InsertOnSubmit(theDrugStoreInfo);
							db.SubmitChanges();
						}
					}
					else
					{
						dbDrugStoreInfo.DrugStoreInfoActivity = theDrugStoreInfo.DrugStoreInfoActivity;
						dbDrugStoreInfo.DrugStoreInfoDetail = theDrugStoreInfo.DrugStoreInfoDetail;
						dbDrugStoreInfo.DrugStoreInfoNews = theDrugStoreInfo.DrugStoreInfoNews;
						dbDrugStoreInfo.LastUpdate = theDrugStoreInfo.LastUpdate;

						db.SubmitChanges();
					}
				}
			}
			catch (Exception exc)
			{
				throw exc;
			}
		}

		#endregion
	}
}
